package com.day19.set;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
public class TreeSetToFileDemo {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> ts = new TreeSet<>((o1, o2) -> {
            int num = o2.getTotal() - o1.getTotal(); // 按总分降序
            if (num == 0) {
                num = o1.getName().compareTo(o2.getName()); // 按姓名升序
            }
            if (num == 0) {
                num = o1.getChinese() - o2.getChinese(); // 按语文成绩降序
            }
            if (num == 0) {
                num = o1.getMath() - o2.getMath(); // 按数学成绩降序
            }
            return num;
        });
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生信息：");
            System.out.print("姓名：");
            String name = sc.nextLine();
            System.out.print("语文成绩：");
            int chinese = sc.nextInt();
            sc.nextLine(); // 清除换行符
            System.out.print("数学成绩：");
            int math = sc.nextInt();
            sc.nextLine(); // 清除换行符
            System.out.print("英语成绩：");
            int english = sc.nextInt();
            sc.nextLine(); // 清除换行符
            Student s = new Student();
            s.setName(name);
            s.setChinese(chinese);
            s.setMath(math);
            s.setEnglish(english);
            ts.add(s);
        }
        sc.close();
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"))) {
            for (Student s : ts) {
                StringBuilder sb = new StringBuilder();
                sb.append(s.getName()).append(",")
                        .append(s.getChinese()).append(",")
                        .append(s.getMath()).append(",")
                        .append(s.getEnglish()).append(",")
                        .append(s.getTotal());
                bw.write(sb.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}